package ssx.demo.uploadfile_demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@RestController
public class Control {

    @RequestMapping(value = "/up",method = RequestMethod.POST)
    public String f(@RequestParam("f") MultipartFile file){
        try {
            String name = file.getOriginalFilename();
            long size = file.getSize();

            File c = c(name);
            file.transferTo(c);
            System.out.println("传输完成："+name);

        } catch (Exception e) {
            e.printStackTrace();
            return "失败";
        }
        return "完成";

    }

    private File c(String fileName){
        boolean mkdir = new File("D:\\mydemo").mkdir();
        File file = new File("D:\\mydemo\\" + fileName);
        try {
            boolean newFile = file.createNewFile();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
}
